﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;


namespace BLL
{
    public partial class LoadCAFunc
    {
        public List<UserEntity> LoadListCAByAll()
        {
            CAService sv = new CAService();
            return sv.LoadListCAByAll();
        }

        public UserEntity LoadCAById(int userid)
        {
            CAService sv = new CAService();
            return sv.LoadCAById(userid);
        }

        public List<CustomerEntity> LoadCurrentCallByCAId(int CAid)
        {
            CAService sv = new CAService();
            var list = sv.LoadCurrentCallsByCAId(CAid);
            foreach (var cus in list)
            {
                cus.EDDS = HelperClass.GetEDDS(cus.EDD);
            }
            return list;
        }

        public void AssignCallsToCA(List<long> CustomersID, int CAid)
        {
            CAService sv = new CAService();
            sv.AssignCallsToCA(CustomersID, CAid);
        }

        public List<CustomerEntity> LoadCustomerForDivide(int sourceid, int collectorid)
        {
            CAService sv = new CAService();
            var list = sv.LoadCustomerForDivide(sourceid, collectorid);
            foreach (var item in list)
            {
                string tmp = HelperClass.GetEDDS(item.EDD);
                item.EDDS = tmp;
            }
            return list;
        }

        public List<vw_DWorking> CountDWList(int sourceid)
        {
            CAService sv = new CAService();
            List<vw_DWorking> tasklist = new List<vw_DWorking>();
            var list = sv.GetDWorkingList().Where(a => a.Source_ID == sourceid).ToList();
            list = list.Where(a => a.CusCallStatus == 2 || a.CusCallStatus == 5).ToList();
            foreach (vw_DWorking item in list)
            {
                if (item.DWCallStatus != null)
                {
                    item.CountDivided = item.CountAll;
                }
                else
                {
                    item.CountUnDivided = item.CountAll;
                }
            }

            var tmplist = list.GroupBy(l => l.Collector_ID).Select(cl => new
            {
                Source_ID = cl.First().Source_ID,
                Collector_ID = cl.Key,
                tmp = cl.ToList().GroupBy(g => g.CusCallStatus).Select(gg => new { 
                    CusCallStatus = gg.Key,
                    Collector_ID = gg.First().Collector_ID,
                    Collector_Code = SharedData.CollectorList.Where(a => a.Collector_ID == gg.First().Collector_ID).SingleOrDefault().Collector_Code,
                    CountDivided = gg.Sum(c => c.CountDivided),
                    CountUnDivided = gg.Sum(c => c.CountUnDivided),
                    CallStatusName = SharedData.CallStatusList.Where(a => a.CallStatus_ID == gg.Key).SingleOrDefault().CallStatusName,
                    CountAll = gg.Sum(c => c.CountDivided) + gg.Sum(c => c.CountUnDivided),
                }),
            }).ToList();

            foreach (var item in tmplist)
            {                             
                foreach (var subitem in item.tmp)
                {
                    vw_DWorking dw = new vw_DWorking();   
                    dw.Source_ID = item.Source_ID;
                    dw.Collector_ID = item.Collector_ID;
                    dw.Collector_Code = subitem.Collector_Code;
                    dw.CusCallStatus = subitem.CusCallStatus;
                    dw.CallStatusName = subitem.CallStatusName;
                    dw.CountDivided = subitem.CountDivided;
                    dw.CountUnDivided = subitem.CountUnDivided;
                    dw.CountAll = subitem.CountAll;
                    tasklist.Add(dw);
                }
            }

            return tasklist;
        
        }

        public List<CallAgentEntity> LoadTotalCallByAllCA()
        { 
            UserService sv = new UserService();
            CAService casv = new CAService();
            var list = new List<CallAgentEntity>();            
            foreach (UserEntity us in sv.LoadUserbyRole(3))
            {
                var tasklist = casv.GetCallByCAId(us.UserID);
                CallAgentEntity agent = new CallAgentEntity();
                agent.ID = us.UserID;
                agent.Name = us.LastName + " " + us.FirstName;
                agent.RegionCode = us.Region_Code;
                agent.TotalC1 = tasklist.Where(a => a.CallStatus_ID == 2).Count();
                agent.TotalC2 = tasklist.Where(a => a.CallStatus_ID == 3).Count();
                agent.TotalE1 = tasklist.Where(a => a.CallStatus_ID == 5).Count();
                agent.TotalE2 = tasklist.Where(a => a.CallStatus_ID == 6).Count();
                agent.TotalKPI = tasklist.Where(a => a.CallStatus_ID == 7).Count();
                agent.TotalCalls = agent.TotalC1 + agent.TotalC2 + agent.TotalE1 + agent.TotalE2 + agent.TotalKPI;
                list.Add(agent);
            }
            return list;
        }

        public bool divideCustomerToCA(List<CustomerEntity> listCusForDivide, List<CallAgentEntity> listCA, string[] listedds, string[] listcallstatus, int Total)
        {
            ImportService sv = new ImportService();
            List<DividingWorkingEntity> listDWE = new List<DividingWorkingEntity>();
            List<CustomerEntity> listTMP = new List<CustomerEntity>();
            List<CustomerEntity> list = new List<CustomerEntity>();
            var selected = listCA.Where(a => a.Selected == true);
            foreach (var callstatus in listcallstatus)
            {
                list.AddRange(listCusForDivide.Where(a => a.CallStatus == int.Parse(callstatus.Trim())));   
            }

            foreach (var edds in listedds)
            {
                var tmp = list.Where(a => a.EDDS == edds.Trim()).ToList();
                listTMP.AddRange(tmp);
            }            

            if (Total >= listTMP.Count)
            {
                listTMP = listTMP.GetRange(0, listTMP.Count);
            }
            else
            {
                listTMP = listTMP.GetRange(0, Total);
            }
            List<CustomerEntity> listCETMP;

            foreach (string callstatus in listcallstatus)
            {
                var listtmpcall = listTMP.Where(a => a.CallStatus == int.Parse(callstatus.Trim())).ToList();
                foreach (string edds in listedds)
                {
                    listCETMP = listtmpcall.Where(a => a.EDDS.Equals(edds.Trim())).ToList();
                    while (listCETMP.Count > 0)
                    {
                        foreach (CallAgentEntity ca in selected.ToList())
                        {
                            if (listCETMP.Count > 0)
                            {
                                DividingWorkingEntity dwe = new DividingWorkingEntity();
                                dwe.CallStatus_ID = int.Parse(callstatus);
                                dwe.Customer_ID = listCETMP[0].Customer_ID;
                                dwe.User_ID = ca.ID;

                                listDWE.Add(dwe);

                                listCETMP.RemoveAt(0);
                            }
                        }
                    }
                }
            }

            return sv.ImportDWE(listDWE);
        }

        public void RemoveCallsByCAId(List<long> ListCustomerID, int CurrentCASelected)
        {
            CAService sv = new CAService();
            sv.RemoveCallsByCAId(ListCustomerID, CurrentCASelected);
        }

        public List<TaskListEntity> LoadCATaskList(int UserID)
        {
            CAService sv = new CAService();
            var list = sv.LoadCATaskList(UserID);
            foreach (var item in list.Reverse<TaskListEntity>())
            {
                if (item.EDD != null)
                {
                    item.EDDM = HelperClass.GetEDDM((DateTime)item.EDD);
                    item.EDDS = HelperClass.GetEDDS(item.EDD);
                }

                DateTime edd, endtime;
                var svtime = ServerDateTime.SystemDateTime;
                if (item.CallEndTime != null)
                {
                    if (item.CallStatusID == 3)
                    {
                        endtime = (DateTime)item.CallEndTime;
                        if ((svtime - endtime).Days < int.Parse(SharedData.C2Policy))
                            list.Remove(item);
                        
                    }
                    else if (item.CallStatusID == 5 || item.CallStatusID == 6)
                    {
                        edd = (DateTime)item.EDD;
                        if (item.CallStatusID == 5 && item.EDDS == "TR3" && (edd - svtime).Days > int.Parse(SharedData.E1Policy))// dieu kien goi E1
                        {
                            list.Remove(item);
                        }
                        else if (item.CallStatusID == 5 && item.EDDS == "TR3" && edd <= svtime)
                        {
                            list.Remove(item);
                        }
                        else if (item.CallStatusID == 6 && item.EDDS == "BORN" && (svtime - edd).Days < int.Parse(SharedData.E2Policy)) // dieu kien goi E2
                        {
                            list.Remove(item);
                        }
                        else if (item.CallStatusID == 6 && item.EDDS == "BORN" && svtime <= edd)
                        {
                            list.Remove(item);
                        }
                    }
                }

            }
            return list;
        }

        public void UpdateCustomer(CustomerEntity cus, ref string message)
        {
            UpdateData data = new UpdateData();
            data.UpdateCustomer(cus, ref message);
        }

    }
}
